﻿using System;
using System.Drawing;
using System.Linq;
using System.Threading;
using JTG.ServiceInterface.InterfaceService;
using Tzw.Common;

namespace JTG.CheckInMachine.Ybfby
{
    public partial class ShowInfo : BaseForm
    {
        private int _remainTimes = App.GlobSettings.MsgShowTimes; //窗口自动关闭时间

        public ShowInfo(Enums.ReportStatus status, int w, int h, IPrinterService printerService, string printName) : base(w, h)
        {
            InitializeComponent();

            base_lab_time.Visible = false;

            switch (status)
            {
                case Enums.ReportStatus.Printing:
                case Enums.ReportStatus.PrintingAndHasReport:
                    new Thread(() =>
                    {
                        while (true)
                        {
                            var printerInfos = printerService.GetList().FirstOrDefault(x=>x.PrinterName==printName);
                            
                            //打开打印机对应的灯光口
                            //TODO

                            //打印完毕后关闭界面
                            if (printerInfos == null || printerInfos.Status != 2)
                            {
                                //关闭
                                Invoke(new Action(CloseWindow));
                                break;
                            }

                            _remainTimes = App.GlobSettings.MsgShowTimes;

                            Thread.Sleep(1000);
                        }
                    }).Start();
                    MusicPlay.PlayMusic(status == Enums.ReportStatus.Printing
                        ? $"{Environment.CurrentDirectory}\\Voice\\1024.768\\打印中无额外报告.mp3"
                        : $"{Environment.CurrentDirectory}\\Voice\\1024.768\\打印机有额外报告.mp3");
                    BackgroundImage = Image.FromFile("Imgs\\1024.768\\报告单打印中.jpg");
                    break;
                case Enums.ReportStatus.IsRepeat:
                    MusicPlay.PlayMusic($"{Environment.CurrentDirectory}\\Voice\\1024.768\\不能重复打印.mp3");
                    BackgroundImage = Image.FromFile("Imgs\\1024.768\\不能重复打印.jpg");
                    break;
                case Enums.ReportStatus.NoPaper:
                    BackgroundImage = Image.FromFile("Imgs\\1024.768\\打印缺纸！.jpg");
                    break;
                case Enums.ReportStatus.Error:
                    BackgroundImage = Image.FromFile("Imgs\\1024.768\\打印机故障，请通知工作人员！.jpg");
                    break;
                case Enums.ReportStatus.NoReport:
                    BackgroundImage = Image.FromFile("Imgs\\1024.768\\没有您的报告单.jpg");
                    break;
                case Enums.ReportStatus.NoResult:
                    BackgroundImage = Image.FromFile("Imgs\\1024.768\\结果未出.jpg");
                    break;

            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            _remainTimes--;
            if (_remainTimes <= 0)
            {
                Close();
            }
        }

        private void CloseWindow()
        {
            //关闭灯光

            Close();
        }
    }
}